﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;

namespace YouDaoNotesCodes
{
    class JsonCreate
    {
        #region JObject和JArry方式，二者使用可参见 - YouDaoNotesCodes.JSON.JArray和JObject封装JSON对象
        public void JsonCreate1()
        {
            JObject obj = new JObject();// Newtonsoft.Json.Linq;
            JObject obj1 = new JObject();

            obj["where"] = obj1;
            obj1["exname"] = "name";//{"exname": "name"}
            obj["skip"] = 0;
            obj["limit"] = 24;

            obj["include"] = new JArray() { "a", "b" };// "include": ["a","b"]
            if (!string.IsNullOrWhiteSpace("keyword"))
            {
                JObject obj2 = new JObject();
                obj1["fileName"] = obj2;
                obj2["like"] = "keyword";

                obj["para"] = new JArray() { obj1["fileName"] };//添加对象数组
            }
            //至此obj1的值为{"exname": "name","fileName": {"like": "keyword"}}
            obj["order"] = "heatCount desc";
            string json = obj.ToString();
            Console.WriteLine(json);
        }
        #endregion

        #region 一种方式就可以生成所有的 JSON：很直观（Dictionary）
        public void JsonCreate2()
        {
            Dictionary<String, Object> data = new Dictionary<String, Object>
            {
                {"code", 0},
                {"message", ""},
                {"data", new List<Object>{
                    new Dictionary<String, Object>{
                            {"provider", "提供者"},
                            {"name", "古生物化石数据"},
                            {"description", new List<Object>{
                                new Dictionary<String, String>{
                                    {"key", "xxx"},
                                    {"value", "xxx"}
                                },
                                new Dictionary<String, String>{
                                    {"key", "xxx"},
                                    {"value", "xxx"}
                                }
                            }},
                            {"download", ""},
                            {"thumbnail", ""},
                            {"detail", ""},
                        }
                    } 
                }
            };

            String json = JsonConvert.SerializeObject(data, Formatting.Indented);
            Console.WriteLine(json);
        }
        #endregion

        #region JsonCreate2方式详解-Dictionary
        public void JsonCreate2_1()
        {
            Dictionary<string, string> Content = new Dictionary<string, string>();
            Content.Add("bulletinContent", "this is bulletinContent");
            Content.Add("topToDay", "this is topToDay");

            //Dictionary转json
            string Contentjson = JsonConvert.SerializeObject(Content);
            //json转Dictionary
            Dictionary<string, string> DicContent = (Dictionary<string, string>)JsonConvert.DeserializeObject(Contentjson.ToString());
        }
        #endregion 

        #region List方式序列化,同实体类赋值序列化-层层赋值
        public void JsonCreate3()
        {
            JavaScriptSerializer se = new JavaScriptSerializer();
            List<Person> people = new List<Person>{
                   new Person{ID = 1, FirstName = "Scott", LastName = "Gurthie"},
                   new Person{ID = 2, FirstName = "Bill", LastName = "Gates"}
                   };


            string jsonString = se.Serialize(people);
            Console.WriteLine(jsonString);
        }
        #endregion

        #region 实体类赋值序列化(常用)
        public string JsonCreate4()
        {
            root rt = new root();
            rt.count = 1;
            List<Person> list = new List<Person>();
            Person ps = new Person();
            ps.ID = 1;
            ps.FirstName = "Gnplums";
            ps.LastName = "Jan";
            list.Add(ps);
            rt.pes = list;

            string seJson = JsonConvert.SerializeObject(rt);
            return seJson;
       }

        
        
        
        #endregion

        #region List方式+struct方式(也可以改为实体类方式，都一样)
        public void createJSONList()
        {
            //创建一个对象数组
            List<BwImageQuery> list = new List<BwImageQuery>();
            BwImageQuery bw = new BwImageQuery();
            bw.XH = "001";
            bw.YWX = "ceshi";
            bw.DanWei = "ESG";
            bw.DWMC = "国际";

            list.Add(bw);

            bw.XH = "001";
            bw.YWX = "ceshi";
            bw.DanWei = "ESG";
            bw.DWMC = "国际";
            list.Add(bw);

            string jarry = JsonConvert.SerializeObject(list);//数组
            string json = JsonConvert.SerializeObject(bw);//对象
            Console.WriteLine(jarry);
            Console.ReadKey();
        }
        #endregion

public struct BwImageQuery
    {
        public string XH { get; set; }
        public string DWMC { get; set; }
        public string DanWei { get; set; }
        public string YWX { get; set; }
    }
    }
    partial class Person
    {
        public int ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
    partial class root
    {
        public List<Person> pes { get; set; }
        public int count { get; set; }
    }
}
